package com.zyj.test.month2.day2;

/**
 * @author Yijia Zhang
 * @create 2024-11-21 22:48
 */
public class Code0006 {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        int loop = (numRows << 1) - 2;
        StringBuilder[] sbs = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            sbs[i] = new StringBuilder();
        }
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int idx = i % loop;
            if (idx < numRows) {
                sbs[idx].append(c);
            } else {
                int more = 1 + idx - numRows;
                sbs[numRows - 1 - more].append(c);
            }
        }
        StringBuilder finalSb = new StringBuilder(s.length());
        for (StringBuilder sb : sbs) {
            finalSb.append(sb.toString());
        }
        return finalSb.toString();

    }
}
